<?
//ทำการแบ่งหน้า
function SplitPage($page="",$totalpage="",$option=""){
	global $ShowSumPages , $ShowPages ;
	// สร้าง link เพื่อไปหน้าก่อน-หน้าถัดไป
	$ShowSumPages .= "<B>กำลังแสดงหน้าที่  </B>";
	if($page>1 && $page<=$totalpage) {
		$prevpage = $page-1;
		$ShowSumPages .= "<a href='".$option."&page=$prevpage' title='Back'><B><-</B></a>\n";
	}
	$ShowSumPages .= " <b>$page/$totalpage</b> ";
	if($page!=$totalpage) {
		$nextpage = $page+1;
		if($nextpage >= $totalpage){
			$nextpage = $totalpage ;
		}
		$ShowSumPages .= "<a href='".$option."&page=$nextpage' title='Next'><B>-></B></a>\n";
	}

	// วนลูปแสดงเลขหน้าทั้งหมด แบบเป็นช่วงๆ ช่วงละ 10 หน้า
	$b=floor($page/10); 
	$c=(($b*10));

	if($c>1) {
		$prevpage = $c-1;
		$ShowPages .= "<a href='".$option."&page=$prevpage' title='10 หน้าก่อนนี้'><<</a> \n";
	}
	else{
		$ShowPages .= "<B><<</B>\n";
	}
	$ShowPages .= " <b>";
	for($i=$c; $i<$page ; $i++) {
		if($i>0)
		$ShowPages .= "<a href='".$option."&page=$i'>$i</a> \n";
	}
	$ShowPages .= "<font color=red>$page</font> \n";
	for($i=($page+1); $i<($c+10) ; $i++) {
		if($i<=$totalpage)
		$ShowPages .= "<a href='".$option."&page=$i'>$i</a> \n";
	}
	$ShowPages .= "</b> ";
	if($c>=0) {
		if(($c+2)<$totalpage){
	$nextpage = $c+10;
	$ShowPages .= "<a href='".$option."&page=$nextpage' title='10 หน้าถัดไป'>>></a> \n";
		}
		else
	$ShowPages .= "<B>>></B>\n";
	}
	else{
		$ShowPages .= "<B>>></B>\n";
	}
}

//แปลงเวลาเป็นภาษาไทย
//ThaiTimeConvert(timestamp,1,1)
function ThaiTimeConvert($timestamp="",$full="",$showtime=""){
	global $SHORT_MONTH, $FULL_MONTH, $DAY_SHORT_TEXT, $DAY_FULL_TEXT;
	$day = date("l",$timestamp);
	$month = date("n",$timestamp);
	$year = date("Y",$timestamp);
	$time = date("H:i:s",$timestamp);
	$times = date("H:i",$timestamp);
	if($full){
		$ThaiText = $DAY_FULL_TEXT[$day]." ที่ ".date("j",$timestamp)." เดือน ".$FULL_MONTH[$month]." พ.ศ.".($year+543);
	}else{
		$ThaiText = date("j",$timestamp)."  ".$SHORT_MONTH[$month]."  ".($year+543);
	}

	if($showtime == "1"){
		return $ThaiText." เวลา ".$time;
	}else if($showtime == "2"){
		$ThaiText = date("j",$timestamp)." ".$SHORT_MONTH[$month]." ".($year+543);
		return $ThaiText." : ".$times;
	}else{
		return $ThaiText;
	}
}

//ตรวจสอบว่าเป็น Admin จริงหรือไม่จริง
function CheckAdmin($user = "", $pwd =""){
	global $db ;
	$db->connectdb(DB_NAME,DB_USERNAME,DB_PASSWORD);
	$res[user] = $db->select_query("SELECT id FROM ".TB_ADMIN." WHERE username='$user' AND password='$pwd' ");
	$arr[user] = $db->fetch($res[user]);
	if(!$arr[user][id]){
		echo "<script language='javascript'>" ;
		echo "alert('ท่านไม่ใช่ผู้ดูแลระบบ')" ;
		echo "</script>" ;
		echo "<script language='javascript'>javascript:history.go(-1)</script>";
		exit();
	}
}

// สร้าง thumbnail ตัดส่วนที่เกิน เพื่อให้ได้ขนาดที่กำหนด
function cropImage( $source , $target , $ssWidth )
{
	
		
	$data= array();
	$size=getimagesize($source);
	$img_w = $size[0];
    $img_h = $size[1];
	
	
	$pMime = $size[mime];
	switch ( $pMime )
	{
		case 'image/gif':
			$imgJpg = imagecreatefromgif( $source );
			$data[ext] = '.gif';
			break;
		case 'image/jpeg':
			$imgJpg = imagecreatefromjpeg( $source );
			$data[ext] = '.jpg';
			break;
		case 'image/png':
			$imgJpg = imagecreatefrompng( $source );
			$data[ext] = '.png';
			break;
		default:
			$data[error] = 'ไม่ใช่รูปภาพ';
			break;
	};
		
		$w=$img_w;
		$h=$img_h;
		if ( $error == '' )
		{
			if ( $w >= $h && $w > $ssWidth )
			{
				$nw = $ssWidth;
				$nh = ( $ssWidth * $h ) / $w;

			}
			else if ( w < h && h > ssWidth )
			{
				$nh = $ssWidth;
				$nw = ( $ssWidth * $w ) / $h;

			}
			else
			{
				$nh = $h;
				$nw = $w;
				
			};
		$img= imagecreatetruecolor($nw,$nh);
		imagecopyresampled($img,$imgJpg,0,0,0,0,$nw,$nh,$img_w,$img_h);
		if ( !imagejpeg($img,$target) )
		{
			$data[error] = "ไม่สามารถสร้างรูปได้, ตรวจสอบไดเร็คทอรี่ให้ถูกต้อง";
		};
		
		imagedestroy($imgJpg);
		imagedestroy($img);
	};
	return $data;
}

// reSize Photo
function reSize($url,$width,$dir){
//$substr=$dir.substr($url,8);
$getSize=getimagesize($url);
$img_w = $getSize[0];
$img_h = $getSize[1];
$w=$img_w;
$h=$img_h;
	if ( $w >= $h && $w > $width )
	{
		$lenght['w'] = $width;
		$lenght['h'] = ( $width * $h ) / $w;
	}
	else if ( $w < $h && $h > $width )
	{
		$lenght['h'] = $width;
		$lenght['w'] = ( $width * $w ) / $h;
	}
	else
	{
		$lenght['h'] = $h;
		$lenght['w'] = $w;
	};
return $lenght;
}

function check_login(){
	if($_SESSION['login']){
		echo '<meta http-equiv="refresh" content="0;url=?m=members&p=dashboard"> ';
	}else{
		return true;
	}
}

//======================== Random Product ID ==========================
function genStr() {
    $length = 10;
    $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
    $string = "";    
    for ($p = 0; $p < $length; $p++) {
        $string .= $characters[mt_rand(0, strlen($characters))];
    }
    return $string;
}

function system_store(){
	return 'PGRpdiBzdHlsZT0iY29sb3I6IzI4MjgyODsiPg0KICAgIDxhIGhyZWY9Imh0dHA6Ly93d3cucG9zdGVydGhhaWxhbmQuY29tIiB0aXRsZT0i4Lil4LiH4Lib4Lij4Liw4LiB4Liy4Lio4Lif4Lij4Li1IOC4peC4h+C5guC4huC4qeC4k+C4suC4n+C4o+C4tSDguKHguLXguITguJnguYDguKvguYfguJnguJfguLHguYjguKfguJvguKPguLDguYDguJfguKggIOC4peC4h+C5guC4huC4qeC4k+C4suC4n+C4o+C4tSwg4Lil4LiH4Lib4Lij4Liw4LiB4Liy4Lio4Lif4Lij4Li1LCDguILguLLguKLguILguK3guIfguKHguLfguK3guKrguK3guIcsIOC4m+C4o+C4sOC4iuC4suC4quC4seC4oeC4nuC4seC4meC4mOC5jCwg4Lij4LmJ4Liy4LiZ4LiE4LmJ4Liy4Lit4Lit4LiZ4LmE4Lil4LiZ4LmMLCDguKPguYnguLLguJnguITguYnguLLguKrguLPguYDguKPguYfguIjguKPguLnguJssIOC5gOC4p+C5h+C4muC4quC5jeC4suC5gOC4o+C5h+C4iOC4o+C4ueC4mywgZWNvbW1lcmNlLCDguILguLLguKLguILguK3guIfguK3guK3guJnguYTguKXguJnguYwsIOC4guC4suC4ouC4guC4reC4h+C4nOC5iOC4suC4meC5gOC4meC5h+C4lSwg4LiC4Liy4Lii4LiC4Lit4LiH4LiX4Liy4LiH4LmA4LiZ4LmH4LiVLCDguKvguLLguIfguLLguJksIOC4quC4oeC4seC4hOC4o+C4h+C4suC4mSwg4LmA4LiB4Lih4Liq4LmMLCDguKHguLfguK3guJbguLfguK3guKHguLfguK3guKrguK3guIcsIOC4o+C4luC4oeC4t+C4reC4quC4reC4hywg4Lij4LiW4Lii4LiZ4LiV4LmM4Lih4Li34Lit4Liq4Lit4LiHLCAg4LiE4Lit4Lih4Lie4Li04Lin4LmA4LiV4Lit4Lij4LmM4Lih4Li34Lit4Liq4Lit4LiHLCDguYLguJnguYnguJXguJrguLjguYrguIQsIOC4geC4peC5ieC4reC4h+C4lOC4tOC4iOC4tOC4leC4reC4pSwg4LiB4Lil4LmJ4Lit4LiH4Lih4Li34Lit4Liq4Lit4LiHLCDguJ7guKPguLDguYDguITguKPguLfguYjguK3guIfguYTguJfguKIsIGdwcywgYmIsIGd1biwg4Lib4Li34LiZ4Lit4Lix4LiU4Lil4LihLCDguJrguLXguJrguLXguIHguLHguJks4LmA4LiL4LmJ4LiH4LiB4Li04LiI4LiB4Liy4LijLCDguYDguJrguK3guKPguYzguKrguKfguKIsIG1wMywgbXA0LCDguKXguIfguYLguIbguKnguJPguLLguILguLLguKLguJrguYnguLLguJnguJ/guKPguLUsIOC4o+C5ieC4suC4meC4hOC5ieC4suC4n+C4o+C4tSwg4Lij4LmJ4Liy4LiZ4LiE4LmJ4Liy4Lit4Lit4LiZ4LmE4Lil4LiZ4LmM4Lif4Lij4Li1LCDguJzguLnguYnguJvguKPguLDguIHguK3guJrguIHguLLguKMsIOC5guC4huC4qeC4k+C4siI+LjwvYT4JCQ0KICAgIDxhIGhyZWY9Imh0dHA6Ly93d3cuY29kZXBvbnBvbi5jb20iPi48L2E+DQo8L2Rpdj4=';
}
?>